﻿@inject IUserService _userService
@inject IHttpService _httpService
@inject IPopupService PopupService

<CnGalWebSite.Components.Containers.MasaTitleContainer Title="@Model.GetPositionName()" Icon="@Model.GetPositionIcon()" Class=" h-100">
    <div class="project-position-card">
        @if (string.IsNullOrWhiteSpace(Model.Type) == false || Model.UrgencyType != PositionUrgencyType.None)
        {
            <div class="name-card">
                @if (string.IsNullOrWhiteSpace(Model.Type) == false)
                {
                    <MChip Small Color="primary">@Model.Type</MChip>
                }
                @if (Model.UrgencyType != PositionUrgencyType.None)
                {
                    <MChip Small Color="primary">@Model.UrgencyType.GetDisplayName()</MChip>
                }
            </div>
        }

        <div class="info-card">
            <div class="item-card">
                <div class="label-large">回报方式</div>
                <div class="title-large primary-text">@Model.BudgetType.GetDisplayName()</div>
            </div>

            @if (Model.BudgetType == BudgetType.Divide || Model.BudgetType == BudgetType.IntervalAndDivide)
            {
                <div class="item-card">
                    <div class="label-large">分成比例</div>
                    <div class="title-large primary-text">@($"{Model.Percentage} %")</div>

                </div>
            }
            @if (Model.BudgetType == BudgetType.Interval || Model.BudgetType == BudgetType.IntervalAndDivide)
            {
                <div class="item-card">
                    <div class="label-large">预算区间</div>
                    <div class="title-large primary-text">@($"￥ {Model.BudgetMin} ~ {Model.BudgetMax}")</div>

                </div>
            }

            <div class="item-card">
                <div class="label-large">截稿日期</div>
                <div class="title-large primary-text">@($"{Model.DeadLine:yyyy-MM-dd}")</div>
            </div>
            <div class="item-card">
                <div class="label-large">应征人数</div>
                <div class="title-large primary-text">@($"{Model.Users.Count} 人")</div>
            </div>
        </div>
        <div class="content-card">
            <CnGalWebSite.ProjectSite.Shared.Components.Shared.IconTitleCard Class="mb-2" Icon="IconType.Infor" Title="详细需求" />
            <div>
                @Model.Description
            </div>
        </div>
        @if (_userService.UserInfo.Id != CreateUserId)
        {
            <div class="action-card">
                <AuthorizeView>
                    <Authorized>
                        <CnGalWebSite.Components.Buttons.MasaButton Rounded="!Model.Users.Any(s=>s.User.Id==_userService.UserInfo.Id)" TextStyle="Model.Users.Any(s=>s.User.Id==_userService.UserInfo.Id)" IsAsync
                                                                    Text="@(Model.Users.Any(s=>s.User.Id==_userService.UserInfo.Id)?"取消应征":"应征")"
                                                                    Icon="@(Model.Users.Any(s=>s.User.Id==_userService.UserInfo.Id)?@IconType.Close.ToIconString():@IconType.Flag.ToIconString())" OnClick="OnClick" />
                    </Authorized>
                    <NotAuthorized>
                        <CnGalWebSite.Components.Buttons.MasaButton Text="登入后应征" Icon="@IconType.Login.ToIconString()" Rounded OnClick="_userService.Login" />
                     </NotAuthorized>
                 </AuthorizeView>

             </div>
        }
    </div>
</CnGalWebSite.Components.Containers.MasaTitleContainer>
<CnGalWebSite.Components.Verifications.DeviceIdentification @ref="deviceIdentification" />

@code {
    [Parameter]
    public ProjectPositionViewModel Model { get; set; }

    [Parameter]
    public EventCallback OnChanged { get; set; }

    [Parameter]
    public string CreateUserId { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    CnGalWebSite.Components.Verifications.DeviceIdentification deviceIdentification;

    public string GetName()
    {
        return (Model.PositionType == ProjectPositionType.Other ? Model.PositionTypeName : Model.PositionType.GetDisplayName()) + " #" + Model.Id;
    }

    public string GetIcon()
    {
        return Model.PositionType switch
        {
            ProjectPositionType.Music => IconType.Music.ToIconString(),
            ProjectPositionType.Painter => IconType.Style.ToIconString(),
            ProjectPositionType.Programmer => IconType.Programmer.ToIconString(),
            ProjectPositionType.Writer => IconType.Writer.ToIconString(),
            ProjectPositionType.CV => IconType.Dub.ToIconString(),
            ProjectPositionType.Other => IconType.Shape.ToIconString(),
            _ => IconType.Shape.ToIconString()
        };
    }

    public async Task OnClick()
    {
        var apply = !Model.Users.Any(s => s.User.Id == _userService.UserInfo.Id);
        var _name = apply ? "应征" : "取消应征";
        try
        {
            var obj = await _httpService.PostAsync<ApplyProjectPositionModel, Result>("api/project/ApplyProjectPosition", new ApplyProjectPositionModel
                {
                    PositionId = Model.Id,
                    Apply = apply,
                    Identification = await deviceIdentification.GetDeviceIdentificationModel()
                });
            //判断结果
            if (obj.Success == false)
            {
                await PopupService.ToastErrorAsync($"{_name}失败", obj.Message);
            }
            else
            {
                await PopupService.ToastSuccessAsync($"{_name}成功");
                await OnChanged.InvokeAsync();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, $"{_name}失败");
        }
    }
}
